<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\ButtonDropdown;
use yii\bootstrap\Modal;

/* @var $this yii\web\View */
/* @var $searchModel app\models\CustomerSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Customer list');
$this->params['breadcrumbs'][] = $this->title;
?>

<style>
	.col-md-10 {
	    padding-left: 0px !important;
	}

	.c-table td{
		padding:5px;
	}

	#advanced_search{
		border: 1px solid #e1e1e8;
		width:420px;
	}

    .modal-dialog {
        margin: 30px auto;
        width: 700px;
    }
</style>

<script>
var grid = null;
var type_search = "basic";

$(document).ready(function() {
	grid = $("#grid").kendoGrid({
        dataSource: {
            transport: {
					read: {
                        url: "/customer/getlist",
                        dataType: "json",
                        type: "post",
                        data : {}
                    },
            },
            pageSize: 25,
            requestStart:function(e){
            	var data = {};

                if(type_search == "basic"){
	            	var data = {
	                	cust_name : $("#name_search").val(),
	                	type_search : type_search
	                };
                }
                else
                {
                    var is_operating_search = 0;
					var inactive = 0;

                    if($("#is_operating_search").is(':checked'))
                        is_operating_search = 1;

                    if($("#inactive_search").is(':checked'))
                    	inactive = 1;

                	data = {
                		cust_type 	: $("#cust_type_search").val(),
                		close_month : $("#close_month_search").val(),
                		ns_person 	: $("#ns_person_search").val(),
                		is_operating : is_operating_search,
                        type_search : type_search,
                        inactive		: inactive
    	            };
                }

            	e.sender.transport.options.read.data = data;
            },
        },
        height: 650,
        filterable: false,
        sortable: false,
        pageable: true,
        columns: [
            {
                title : "<?= Yii::t('app', 'Customer ID') ?>",
                field : "cust_id",
                width : 100,
                template: "<a href='/customer/update?id=${cust_id}'>${cust_id}</a>"
            },
            {
                field: "cust_name",
                title: "<?= Yii::t('app', 'Customer name') ?>",
                width : 100,
            },
            {
                field: "address",
                title: "<?= Yii::t('app', 'Address') ?>",
            },
            {
                field: "person_in_charge ",
                title: "<?= Yii::t('app', 'CL person') ?>",
                width : 150,
            },
            {
                field: "email",
                title: "<?= Yii::t('app', 'Email') ?>",
                width : 150,
            },
            {
                field: "tel",
                title: "<?= Yii::t('app', 'Tel') ?>",
                width : 100,
            },
            {
                field: "ns_person",
                title: "<?= Yii::t('app', 'NS person') ?>",
                width : 100,
            },
            {
                field: "advisory_fee",
                title: "<?= Yii::t('app', 'Advisory fee') ?>",
                width : 100,
            },
            {
                field: "close_fee",
                title: "<?= Yii::t('app', 'Close fee') ?>",
                width : 100,
            },
            {
                field: "close_month",
                title: "<?= Yii::t('app', 'Close month') ?>",
                width : 100,
            },
            {
                field: "other_fee_amount",
                title: "<?= Yii::t('app', 'Number of cases') ?>",
                width : 120,
                template : "${other_fee_amount}<?= Yii::t('app', 'Matter') ?>"
            },
            {
                field: "",
                title: "<?= Yii::t('app', 'Signup PDF') ?>",
                width : 90,
                template:'有                '
            },
        ]
    });

	initCustTypeSearch();

    $('#modalButton').click(function (){
        $('#modal').modal('show')
            .find('#modalContent')
            .load($(this).attr('value'));
    });
});

function search(){
	 type_search = "basic";
	 $("#grid").data("kendoGrid").dataSource.read();	// Reset grid
}

function searchAdvanced(){
	type_search = "advanced";
	 $("#grid").data("kendoGrid").dataSource.read();	// Reset grid
}

function showAdvancedSearch(){
	if($("#advanced_search").css("display") == "none")
		$("#advanced_search").slideDown(400);
	else
		$("#advanced_search").slideUp(400);
}

function initCustTypeSearch(){
	$("#cust_type_group.btn-group > .btn").click(function(){
	    $(this).addClass("active").siblings().removeClass("active");
	    $("#cust_type_search").val(this.innerHTML);
	});
}

</script>
<?php
	$monthList = [];

	for($i=1; $i<=12; $i++){
		$monthList[$i] = $i . "月";
	}
?>
<div class="customer-index">

    <div class="c-header1"><?= Html::encode($this->title) ?></div>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


	<div class="row">
	  <div class="col-lg-6" style="width:450px;">
	    <div class="input-group">
	      <input id="name_search" type="text" class="form-control">
	      <span class="input-group-btn">
	        <button type="button" class="btn btn-default" onclick="search();">
		  		<span class="glyphicon glyphicon-search"></span>
			</button>
			 <button type="button" class="btn btn-default" onclick="showAdvancedSearch();">
		  		<span class="glyphicon glyphicon-circle-arrow-down"></span>
			</button>
	      </span>
	    </div><!-- /input-group -->
	  </div><!-- /.col-lg-6 -->

	   <div style="float:right; margin-right:20px;">
    <div class="input-group">

	      <?= Html::a(Yii::t('app', 'Add'), ['create'], ['class' => 'btn btn-success']) ?>
          <?= Html::button(
              'create',
              ['value' => '/customer/create1',
                  'id' => 'modalButton'

              ]) ?>
    </div><!-- /input-group -->
  </div><!-- /.col-lg-6 -->

	</div><!-- /.row -->

    <div id="advanced_search" style="background-color: white; padding:5px; display:none; position: absolute; top: 93px; z-index:10;" >
    	<table class="c-table" style="width:100%">
    		<tr>
    			<td><label><?= Yii::t('app', 'Corporation') ?>/<?= Yii::t('app', 'Individual') ?>/<?= Yii::t('app', 'Spot') ?></label></td>
    			<td>
    				<div id="cust_type_group" class="col-md-10 btn-group">
						  <button class="btn btn-default active" type="button">法人</button>
						  <button class="btn btn-default" type="button">個人</button>
						  <button class="btn btn-default" type="button">スポット</button>
						  <input type="hidden" id="cust_type_search" value="法人">
					</div>
    			</td>
    		</tr>
    		<tr>
    			<td><label><?= Yii::t('app', 'Close month') ?></label></td>
    			<td>
    				<select class="form-control" id="close_month_search" style="width:150px;">
    				<?php foreach($monthList as $key => $value){?>
						<option value="<?= $key ?>"><?= $value ?></option>
						<?php }?>
					</select>
				</td>
    		</tr>
    		<tr>
    			<td><label><?= Yii::t('app', 'NS person') ?></label></td>
    			<td>
    				<select class="form-control" id="ns_person_search" style="width:150px;">
    				<?php foreach($nsPersonList as $item){?>
						<option><?= $item['ns_person'] ?></option>
						<?php }?>
					</select>
    			 </td>
    		</tr>
    		<tr>
    			<td><label for="is_operating_search"><?= Yii::t('app', 'Is operating') ?></label></td>
    			<td><input type="checkbox" id="is_operating_search" /></td>
    		</tr>
    		<tr>
    			<td><label for="inactive_search"><?= Yii::t('app', 'Inactive') ?></label></td>
    			<td><input type="checkbox" id="inactive_search" /></td>
    		</tr>
    		<tr>
    			<td colspan="2">
    				<span class="input-group-btn">
			        <button type="button" class="btn btn-default" onclick="searchAdvanced();">
				  		<span class="glyphicon glyphicon-search"></span>
					</button>
			      </span>
    			</td>
    		</tr>
    	</table>
    </div>

    <?php
    Modal::begin([
        'id' => 'modal'
    ]);

    echo "<div id='modalContent'></div>";

    Modal::end();
    ?>

	<div id="grid" style="margin-top:20px;">
	</div>
</div>
